<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/23 16:21
 */

namespace App\Project\Filters;

class HpiSubFilter extends FilterAbstract
{
    protected function forExtend()
    {
        $map = [
            'before_treatment.fbg.low'    => 1,
            'before_treatment.fbg.high'   => 1,
            'before_treatment.2hpbg.low'  => 1,
            'before_treatment.2hpbg.high' => 1,
            'before_treatment.hba1c'      => 1,
            'after_treatment.fbg.low'     => 1,
            'after_treatment.fbg.high'    => 1,
            'after_treatment.2hpbg.low'   => 1,
            'after_treatment.2hpbg.high'  => 1,
        ];

        $map['symptoms']      = $this->symptoms($this->request->input('symptoms'));
        $map['diagnose']      = $this->diagnose($this->request->input('diagnose'));
        $map['medication']    = $this->medication($this->request->input('medication'));
        $map['cn_medication'] = $this->cnMedication($this->request->input('cn_medication'));

        return $map;
    }

    public function symptoms($symptoms)
    {
        if (is_array($symptoms)) {
            return array_pad([], count($symptoms), 1);
        }

        return 1;
    }

    protected function diagnose($diagnose)
    {
        if (is_array($diagnose)) {
            return array_pad([], count($diagnose), 1);
        }

        return 1;
    }

    protected function medication($medication)
    {
        $map = [];

        if (is_array($medication)) {
            foreach ($medication as $key => $item) {
                $map[$key] = array_flip([
                    'name', 'dosage.type', 'dosage.value', 'dosage.values.morning', 'dosage.unit', 'start_time',
                    'end_time', 'usage', 'frequency', 'remark', 'dosage.values.atnoon',
                    'dosage.values.evening', 'dosage.values.before_sleep'
                ]);
            }
        }

        return $map;
    }

    protected function cnMedication($cn_medication)
    {
        $map = [];

        if (is_array($cn_medication) && ($count = count($cn_medication)) > 0) {

            $keys = [];

            for ($i = 0; $i < $count; $i++) {

                if ($cn_medication[$i]['type'] == 1) {
                    $drugs = count($cn_medication[$i]['drugs']);

                    for ($j = 0; $j < $drugs; $j++) {
                        $keys[$j] = array_flip(['name', 'weight']);
                    }

                    $map[$i] = array_merge(array_flip(['usage', 'dosage', 'frequency', 'dose', 'type']), ['drugs' => $keys]);
                } elseif ($cn_medication[$i]['type'] == 3) {
                    $map[$i] = array_flip([
                        'name', 'start_time', 'end_time', 'times_per_day', 'quantity_per_times',
                        'quantity_unit', 'total', 'type'
                    ]);
                }
            }
        }

        return $map;
    }

    protected function getRootFieldsHasNoSon()
    {
        return [
            'id',
            'record_id',
            'started_at',
            'inducement',
            'symptoms',
            'clinic_site',
            'diagnose',
            'medication',
            'before_treatment',
            'after_treatment',
            'type'
        ];
    }

    protected function getRootFieldsHasSon()
    {
        return [];
    }

    protected function getSonFieldsForEachRoot()
    {
        return [];
    }
}